package com.wocao.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wocao.entity.ResponseResult;
import com.wocao.entity.ZyBuilding;
import com.wocao.service.ZyBuildingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigInteger;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Random;

/**
 * 楼栋  前端控制器
 *
 * @since 2023-11-29
 */
@RestController
@RequestMapping("/zyBuilding")
public class ZyBuildingController {

    @Autowired
    private ZyBuildingService zyBuildingService;


    @GetMapping("buildings")
    public ResponseResult<Page<ZyBuilding>> findJointPage(@RequestParam(required = false, defaultValue = "1") Long current,
                                                          @RequestParam(required = false, defaultValue = "2") Long size,
                                                          @RequestParam(required = false, defaultValue = "") String buildingName,
                                                          @RequestParam(required = false, defaultValue = "") String buildingCode,
                                                          @RequestParam(required = false, defaultValue = "") String communityId) {
        try {
            Page<ZyBuilding> page = zyBuildingService.getBuildingsByLike(new Page<>(current, size), buildingName, buildingCode, communityId);
            List<ZyBuilding> records = page.getRecords();
            for (ZyBuilding record : records) {
                LocalDateTime createTime = record.getCreateTime();
                String formattedCreateTime = createTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
                record.setFormattedCreateTime(formattedCreateTime);
            }
            return ResponseResult.ok(page);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error(500l, "查询失败:" + e.getMessage());
        }
    }

    @PostMapping("building/{username}")
    public ResponseResult<String> addBuilding(@RequestBody ZyBuilding building, @PathVariable String username) {
        try {
            Random random = new Random();
            String prefix = "BUILDING_170";
            StringBuilder sb = new StringBuilder(prefix);

            for (int i = 0; i < 10; i++) {
                int digit = random.nextInt(10);
                sb.append(digit);
            }

            building.setBuildingCode(sb.toString());
            building.setCreateBy(username);
            building.setCreateTime(LocalDateTime.now());
            zyBuildingService.save(building);
            return ResponseResult.ok("ok");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error(500l, "添加失败:" + e.getMessage());
        }
    }

    @PutMapping("building/{username}")
    public ResponseResult<String> updateBuilding(@RequestBody ZyBuilding building, @PathVariable String username) {
        try {
            building.setUpdateBy(username);
            building.setUpdateTime(LocalDateTime.now());
            zyBuildingService.updateById(building);
            return ResponseResult.ok(building);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error(500l, "修改失败:" + e.getMessage());
        }
    }

    @DeleteMapping("building/{buildingIds}")
    public ResponseResult<String> deleteBuilding(@PathVariable List<String> buildingIds) {

        try {
            zyBuildingService.removeByIds(buildingIds);
            return ResponseResult.ok("ok");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error(500l, "删除失败:" + e.getMessage());
        }
    }

    @GetMapping("export")
    public void exportExcel(HttpServletResponse response, @RequestParam("buildingIds") String[] buildingIds) {
        try {
            zyBuildingService.export(response, buildingIds);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //单元下拉框查询的楼栋信息
    @GetMapping("selectBuildingAlls")
    public ResponseResult selectBuildingAlls() {
        return ResponseResult.ok(zyBuildingService.list());
    }


    @GetMapping("buildings/{communityId}")
    public ResponseResult<ZyBuilding> findBuildingsByComId(@PathVariable String communityId) {
        List<ZyBuilding> buildingList = zyBuildingService.getBuildingsByComId(communityId);
        return ResponseResult.ok(buildingList);
    }
}
